package cn.bopomo.dealer.base.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityUtil {

	private static final String MD5 = "MD5";
	
	private SecurityUtil(){}
	
	public static String MD5(String content){
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance(MD5);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
		md5.update(content.getBytes());
		return byteToHex(md5.digest()).replaceAll(":","");
	}
	
	private static String byteToHex(byte[] bytes){
		String hs="";
	    String stmp="";
	    for (int n=0;n<bytes.length;n++)
	     {
	      stmp=(java.lang.Integer.toHexString(bytes[n] & 0XFF));
	      if (stmp.length()==1) hs=hs+"0"+stmp;
	      else hs=hs+stmp;
	      if (n<bytes.length-1)  hs=hs+":";
	     }
	    return hs;
	}
	
	public static String[] genPass(int amount, int length){
		String[] base = new String[]{
			"0","1","2","3","4","5","6","7","8","9",
			"a","b","c","d","e","f","g","h","i","j",
			"k","l","m","n","o","P","Q","R","S","T",
			"U","V","W","X","Y","Z","A","B","C","D","E",
			"F","G","H","I","J","K","L","M","N","O",
			"p","q","r","s","t","u","v","w","x","y","z"
		};
		
		String temp;
		String[] passes = new String[amount];
		
		for(int j=0; j<amount; j++){
			StringBuffer sb = new StringBuffer();
			for(int i=0; i<length; i++){
				temp = base[(int)(10*Math.random())];
				if( i==0 && temp.equals("0"))
					i--;
				else
					sb.append(temp);
			}
			passes[j] = sb.toString();
		}
		
		return passes;
	}
	
	public static String genRandom(int length){
		String[] base = new String[]{
				"0","1","2","3","4","5","6","7","8","9"};
		String temp;	
		StringBuffer sb = new StringBuffer();
		for(int i=0; i<length; i++){
			temp = base[(int)(10*Math.random())];
			if( i==0 && temp.equals("0"))
				i--;
			else
				sb.append(temp);
		}
					
		return sb.toString();		
	}
}
